package com.web.user.member.ao.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;













import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.Kongjian;
import com.base.kongjian.entity.Message;
import com.base.kongjian.entity.WealthSystem;
import com.base.kongjian.service.KongjianService;
import com.base.kongjian.service.MessageService;
import com.base.kongjian.service.WealthSystemService;
import com.base.user.entity.Friends;
import com.base.user.entity.GZ;
import com.base.user.entity.Member;
import com.base.user.service.FriendsService;
import com.base.user.service.GZService;
import com.base.user.service.MemberService;
import com.mysteel.util.URLEncoder;
import com.web.user.member.ao.MemberAo;
import com.web.user.member.utils.CookieUtils;
import com.web.user.member.utils.IPUtils;
import com.web.user.member.utils.LoginStatusThread;

@Service("memberAo")
public class MemberAoimpl implements MemberAo
{
	private static String DOMIN = "kongjianxiu.xicp.net";

	private static Map<String, Thread> threadMap;
	
	static
	{
		threadMap = new HashMap<String,Thread>();
	}
	

	@Autowired
	private MemberService memberService;
	@Autowired
	private KongjianService kongjianService;
	@Autowired
	private GZService gzService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private FriendsService friendsService;
	@Autowired
	private WealthSystemService wealthSystemService;
	
	@Override
	public String validate(Map<String, Object> params)
	{
		Member  m = null;
		
		if(ObjectUtils.notEmpty (params))
		{
			m = memberService.getMemberByMap (params);
		}
		
		if(ObjectUtils.isNull (m))
		{
			return "notExit";
		}
		
		return "exit";
	}

	@Override
	public void register(Member member)
	{
		member.setUserName (member.getEmail ());
		memberService.register (member);
		
		//ע��ɹ������ռ�
		Kongjian kj = new Kongjian ();
		
		String email =  member.getEmail ();
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("email" ,email);
		Member m = memberService.getMemberByMap (params);
		if(ObjectUtils.notNull (m))
		{
			kj.setMemberId (m.getId ());
			
			kongjianService.saveKJ (kj);
		}
		
	}

	@Override
	public Map<String, Object> login(HttpServletRequest request, Map<String, Object> params,HttpServletResponse response)
	{
		Map<String, Object> returnMap = new HashMap<String,Object>();
		
		
		String userName = (String) params.get ("userName");
		String password = (String) params.get ("password");
		
		long id = 0;
		try{
			
			id= Long.valueOf (params.get ("id").toString ());
		}
		catch(Exception e)
		{
			id = 0;
		}
		
		String email = userName ;
		String lastLoginIp = IPUtils.getRemoteHost(request);
		Map<String, Object> para = new HashMap<String,Object>();
		
		para.put ("userName" ,userName);
		para.put ("password" ,password);
		para.put ("email" ,email);
		para.put ("id" ,id);
		para.put ("lastLoginIp" ,lastLoginIp);
		
		
		Map<String, Object> map = memberService.login (para);
		
		Member member = (Member)map.get ("member");
		
		if(ObjectUtils.notNull (member))
		{
			String nickName = URLEncoder.encode (member.getNickName ());
			returnMap.put ("loginMessage" ,"��¼�ɹ�");
			
			CookieUtils.setCookieValue (request ,response ,"member_id" ,member.getId()+"" ,DOMIN ,-1);
			CookieUtils.setCookieValue (request ,response ,"userName" ,member.getUserName ()+"" ,DOMIN ,-1);
			CookieUtils.setCookieValue (request ,response ,"email" ,member.getEmail ()+"" ,DOMIN ,-1);
			CookieUtils.setCookieValue (request ,response ,"nickName" ,nickName+"" ,DOMIN ,-1);
			CookieUtils.setCookieValue (request ,response ,"login_type_id" ,"has_login" ,DOMIN ,-1);
			HttpSession session = request.getSession ();
			session.setMaxInactiveInterval (1);

			LoginStatusThread lst = new LoginStatusThread (this ,request ,response ,member.getId (),memberService);
			lst.start ();
			threadMap.put (""+member.getId () ,lst);
			
			return returnMap;
		}
		
		returnMap.put ("loginMessage" ,map.get ("loginMessage"));
		
		return returnMap;
	}

	@Override
	public void logOut(HttpServletRequest request,HttpServletResponse response, long memberId)
	{
		Member m = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (m))
		{
			/**
			 * cookie ���浽����
			 */
			CookieUtils.setCookieValue (request ,response ,"member_id" ,null ,DOMIN ,0);
			CookieUtils.setCookieValue (request ,response ,"userName" ,null ,DOMIN ,0);
			CookieUtils.setCookieValue (request ,response ,"email" ,null ,DOMIN ,0);
			CookieUtils.setCookieValue (request ,response ,"nickName" ,null ,DOMIN ,0);
			CookieUtils.setCookieValue (request ,response ,"login_type_id" ,null ,DOMIN ,0);
			threadMap.remove (""+memberId); //�ͷ��ڴ�
			memberService.logOut (memberId); //����
		}
	}

	@Override
	public String validateHashLogin(long memberId,HttpServletRequest request,HttpServletResponse response)
	{
		Member member = memberService.getMemberById (memberId);
		String loginIp = IPUtils.getRemoteHost(request);
		
		if(ObjectUtils.notNull (member))
		{
			if(loginIp.equals (member.getLastLoginIp ())&&member.getLoginStatus ()==1)
			{
				LoginStatusThread thread = (LoginStatusThread) MemberAoimpl.threadMap.get (""+memberId);
				if(ObjectUtils.notNull (thread))
				{
					thread.flag = "online";
				}
				
				return "normal";
			}else if(member.getLoginStatus ()==2)
			{
				logOut (request ,response ,memberId);
				
				return "Abnormal account";//�˺��쳣
			}
			
			else
			{
				logOut (request ,response ,memberId);
				return "A long-distance login";//��ص�½
			}
		}
		logOut (request ,response ,memberId);
		
		return "Abnormal account";//�˺��쳣
	}

	@Override
	public void addGz(long friend_id, HttpServletRequest request)
	{
		GZ gz = new GZ ();
		long memberId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		gz.setMemberId (memberId);
		gz.setFriend_id (friend_id);
		
		gzService.addGZ (gz);
	}

	@Override
	public void addFriends(HttpServletRequest request, long friend_id)
	{
		long memberId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		Member m = memberService.getMemberById (memberId);
		
		Message message = new Message ();
		message.setContent (m.getNickName ()+"�������Ϊ���ѣ�");
		message.setMemberId (friend_id);
		message.setSend_MId (memberId);
		message.setStatus (1);
		message.setType (2);
		
		messageService.sendMessage (message);
	}

	@Override
	public void requestPass(HttpServletRequest request, long send_MId)
	{
		String str = CookieUtils.getCookieValue(request, "member_id");
		long memberId = 0L;
		if(!StringUtils.isTrimEmpty(str))
		{
			memberId = Long.parseLong(str);
		}
		
		Member member = memberService.getMemberById(memberId);
		String nickName = "";
		if(ObjectUtils.notNull(member))
		{
			nickName = member.getNickName();
		}
		
		Message message = new Message();
		Friends f1 = new Friends();
		Friends f2 = new Friends();
		
		f1.setFriend_id(send_MId);
		f1.setMemberId(memberId);
		f2.setFriend_id(memberId);
		f2.setMemberId(send_MId);
		
		friendsService.addFriends(f2);
		friendsService.addFriends(f1);
		
		message.setMemberId(send_MId);
		message.setSend_MId(memberId);
		message.setStatus(1);
		message.setType(4);
		message.setContent(nickName+"�Ѿ������Ϊ���ѣ�");
		messageService.sendMessage(message);//��֤ͨ��������Ϣ!
		
		WealthSystem ws = wealthSystemService.getWSByMemberId(memberId);
		
		if(ObjectUtils.notNull(ws))
		{
			ws.setFansNum(ws.getFansNum()+1);
			wealthSystemService.updateWS(ws);
		}else
		{
			ws = new WealthSystem();
			ws.setFansNum(1);
			ws.setMemberId(memberId);
			wealthSystemService.saveWS(ws); //�Ƹ�ϵͳfan������һ
		}
		
		WealthSystem ws_send = wealthSystemService.getWSByMemberId(send_MId);
		
		if(ObjectUtils.notNull(ws_send))
		{
			ws_send.setFansNum(ws_send.getFansNum()+1);
			wealthSystemService.updateWS(ws_send);
		}else
		{
			ws_send = new WealthSystem();
			ws_send.setFansNum(1);
			ws_send.setMemberId(send_MId);
			wealthSystemService.saveWS(ws_send); //�Ƹ�ϵͳfan������һ
		}
	}
}
